package cn.webrx;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class List5 {
    public static void main(String[] args) {
        //User对象
        List<User> us = new ArrayList<>();
        us.add(new User(1,"李四",95));
        us.add(new User(21,"张三丰",90));
        us.add(new User(32,"王五",45));
        us.add(new User(5,"李丽",70));
        us.add(new User(7,"张三",60));
        System.out.println(us);
        Collections.sort(us,(a,b)->a.getId()-b.getId());
        Collections.shuffle(us);
        System.out.println(us);
        Collections.sort(us,(a,b)->a.getName().compareTo(b.getName()));
        System.out.println(us);
        us.sort((a,b)->b.getScore()-a.getScore());
        System.out.println(us);



        System.out.println((int)'张');
        System.out.println((int)'李');
    }

    public static void sort2(String[] args) {


        //数字排序
        List<Integer> num = new ArrayList<>(List.of(10, 2, 9, 30, 18, 6, 1));
        System.out.println(num);
        num.sort((a,b)->a-b);//升序
        System.out.println(num);
        num.sort((a,b)->b-a); //降序
        System.out.println(num);
        Collections.shuffle(num);//洗牌
        Collections.shuffle(num);
        Collections.shuffle(num);
        System.out.println(num);
        Collections.sort(num);//升序
        Collections.sort(num,(a,b)->a-b);//升序
        Collections.sort(num,(a,b)->b-a);//降序
        System.out.println(num);

        //字符串排序
        List<String> list = new ArrayList<>(6);
        list.addAll(List.of("go","java","javascript","c","basic","c#","php","python"));
        System.out.println(list);
        Collections.sort(list);
        System.out.println(list);
        Collections.sort(list,(a,b)->a.compareTo(b));
        System.out.println(list);
        Collections.sort(list,(a,b)->b.compareTo(a));
        System.out.println(list);
        Collections.shuffle(list);//洗牌
        System.out.println(list);

        list.add(0,"Ab");
        Collections.sort(list,(a,b)->a.length()-b.length());
        System.out.println(list);
        Collections.sort(list,(a,b)->b.length()-a.length());
        System.out.println(list);
    }


    public static void test(String[] args) {
        //List<int> list1; 错误的，因为泛型编程，基本的类型
        List<int[]> list2;
        List<Integer> list3;
        List<User> list4;
    }
}

class User {
    private int id;
    private String name;
    private int score;

    public User() {
    }

    public User(int id, String name, int score) {
        this.id = id;
        this.name = name;
        this.score = score;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", score=" + score +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
}
